package com.esender.placeorder.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    // 下单服务的交换机
    public static final String PLACE_ORDER_EXCHANGE = "place_order_exchange";

    // 三个服务的queue
    public static final String COUPON_QUEUE = "coupon_queue";
    public static final String USER_POINTS_QUEUE = "user_points_queue";
    public static final String BUSINESS_QUEUE = "business_queue";

    /**
     * 声明一个交换机
     * @return
     */
    @Bean
    public Exchange placeOrderExchange() {

        return ExchangeBuilder.fanoutExchange(PLACE_ORDER_EXCHANGE).build();
    }

    /**
     * 声明优惠券服务的队列
     * @return
     */
    @Bean
    public Queue couponQueue() {
        return QueueBuilder.durable(COUPON_QUEUE).build();
    }

    /**
     * 声明用户积分服务的队列
     * @return
     */
    @Bean
    public Queue userPointsQueue() {
        return QueueBuilder.durable(USER_POINTS_QUEUE).build();
    }

    /**
     * 声明商家服务的队列
     * @return
     */
    @Bean
    public Queue businessQueue() {
        return QueueBuilder.durable(BUSINESS_QUEUE).build();
    }

    /**
     * 绑定交换机和队列
     * @return
     */
    @Bean
    public Binding bindCouponQueue(Exchange placeOrderExchange,Queue couponQueue) {
        return BindingBuilder.bind(couponQueue).to(placeOrderExchange).with("").noargs();
    }
    @Bean
    public Binding userPointsBinding(Exchange placeOrderExchange,Queue userPointsQueue){
        return BindingBuilder.bind(userPointsQueue).to(placeOrderExchange).with("").noargs();
    }
    @Bean
    public Binding businessBinding(Exchange placeOrderExchange,Queue businessQueue){
        return BindingBuilder.bind(businessQueue).to(placeOrderExchange).with("").noargs();
    }
}
